home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / HyperCard CTB Toolkit 1.0b2 / Source Code / CTBNewConnection.p < prev    next >
Encoding:
Text File  |  1995-02-07  |  4.0 KB  |  132 lines  |  [TEXT/MPS ]

  1. (*
  2.     CTBNewConnection [tool[,inBufSz[,outBfSz]]] -- Make a new connection. If one is open, dispose of it.
  3.         The tool parameter is the initial tool name to use. The inBufSz and outBufSz parameters are the
  4.         sizes of the input and output buffers to allocate.
  5.  
  6.     To compile and link this file using Macintosh Programmer's Workshop,
  7.  
  8.         pascal -w CTBNewConnection.p
  9.         link -m ENTRYPOINT -o HyperCommands -rt XCMD=2752 -sn Main=CTBNewConnection ∂
  10.             CTBNewConnection.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
  11.  
  12.     © Copyright 1990 by Apple Computer, Inc.
  13.  
  14.     Initial coding 2/90 by Harry R. Chesley.
  15. *)
  16.  
  17. {$R-}
  18.  
  19. {$S CTBNewConnection }     { Segment name must be the same as the command name. }
  20.  
  21. unit DummyUnit;
  22.  
  23. interface
  24.  
  25. uses MemTypes, QuickDraw, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf, HyperXCmd;
  26.  
  27. procedure EntryPoint(paramPtr: XCmdPtr);
  28.     
  29. implementation
  30.  
  31. procedure CTBNewConnection(paramPtr: XCmdPtr); forward;
  32.  
  33. procedure EntryPoint(paramPtr: XCmdPtr);
  34.  
  35.     begin
  36.         CTBNewConnection(paramPtr);
  37.     end;
  38.  
  39. procedure CTBNewConnection(paramPtr: XCmdPtr);
  40.  
  41.     {$I CTBUtil.inc}
  42.  
  43.     var i, j: integer;
  44.         szs: CMBufferSizes;
  45.         szsIndx: CMBufFields;
  46.         toolName: Str255;
  47.         procID: integer;
  48.         theBuf: InputBufferHandle;
  49.         cHand: ConnHandle;
  50.  
  51.     procedure Fail(errMsg: Str255); { set theResult and quit }
  52.         begin
  53.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  54.             exit(CTBNewConnection);
  55.         end;
  56.  
  57.     begin
  58.         { Verify the number of parameters. }
  59.         if paramPtr^.paramCount > 3 then Fail('Invalid parameter count');
  60.  
  61.         { Check that the Comm Toolbox is present and ready. }
  62.         CTBReady;
  63.  
  64.         { Get the buffer sizes. }
  65.         for szsIndx := cmDataIn to cmRsrvOut do szs[szsIndx] := 0;
  66.         if ParmPresent(2) then szs[cmDataIn] := GetLongParm(1);
  67.         if ParmPresent(3) then szs[cmDataOut] := GetLongParm(2);
  68.  
  69.         { If there's already a connection allocated, get rid of it. }
  70.         if Globals^^.connHand <> nil then
  71.             begin
  72.                 { Dispose of the connection itself. }
  73.                 theBuf := InputBufferHandle(CMGetUserData(Globals^^.connHand));
  74.                 CMDispose(Globals^^.connHand);
  75.                 { Get rid of the input buffer. }
  76.                 DisposHandle(Handle(theBuf));
  77.                 { Remove it from the outstanding tools list. }
  78.                 j := 1;
  79.                 for i := 1 to Globals^^.allToolsSize do
  80.                     if Globals^^.allTools[i].cHand <> Globals^^.connHand then
  81.                         begin
  82.                             Globals^^.allTools[j] := Globals^^.allTools[i];
  83.                             j := j+1;
  84.                         end;
  85.                 Globals^^.allToolsSize := Globals^^.allToolsSize-1;
  86.                 SetHandleSize(Handle(Globals),sizeof(OurGlobalType)-sizeof(ToolArray)+
  87.                                             Globals^^.allToolsSize*sizeof(OneToolType));
  88.                 Globals^^.connHand := nil;
  89.             end;
  90.  
  91.         { Find the proc ID. }
  92.         if ParmPresent(1) then GetStrParm(1,toolName)
  93.         else toolName := 'Serial Tool';
  94.         { Get the ID. }
  95.         procID := CMGetProcID(toolName);
  96.         if procID = -1 then
  97.             begin
  98.                 { If we can't get what we want, try to get what we need. }
  99.                 if CRMGetIndToolName(ClassCM,1,toolName) <> noErr then procID := -1
  100.                 else procID := CMGetProcID(toolName);
  101.                 { If there's not tools at all, fail. }
  102.                 if procID = -1 then Fail('Cannot find a default tool');
  103.             end;
  104.  
  105.         { Allocate an input buffer. }
  106.         theBuf := InputBufferHandle(NewHandle(sizeof(InputBufferType)));
  107.         if theBuf = nil then Fail('Could not create buffer');
  108.         MoveHHi(Handle(theBuf));
  109.         HLock(Handle(theBuf));
  110.         theBuf^^.amountLeft := 0;
  111.         theBuf^^.timeOut := -1;
  112.         theBuf^^.termString := nil;
  113.         { Create the new connection handle. }
  114.         cHand := CMNew(procID,cmData+cmDataClean+cmNoMenus+cmQuiet,szs,0,ord4(theBuf));
  115.         { Did we make it? }
  116.         if cHand = nil then
  117.             begin
  118.                 DisposHandle(Handle(theBuf));
  119.                 Fail('Could not create new connection record');
  120.             end;
  121.         { Save it. }
  122.         Globals^^.connHand := cHand;
  123.         { Add it to the outstanding tools list. }
  124.         Globals^^.allToolsSize := Globals^^.allToolsSize+1;
  125.         SetHandleSize(Handle(Globals),sizeof(OurGlobalType)-sizeof(ToolArray)+
  126.                                     Globals^^.allToolsSize*sizeof(OneToolType));
  127.         Globals^^.allTools[Globals^^.allToolsSize].tType := connectionTool;
  128.         Globals^^.allTools[Globals^^.allToolsSize].cHand := Globals^^.connHand;
  129.     end;
  130.  
  131. end.
  132.